<?php
App::uses('Component', 'Controller');
class GameComponent extends Component {
    public function doCheckWinner($price, $guess1, $guess2, $guess3, $guess4) {
        $listLowerEqual = array();
		$listHigher = array();
		$listResult = array();
		
		$listGuess = array($guess1, $guess2, $guess3, $guess4);
		
		if($this->hasNotNumberParam($price, $listGuess)){
			return json_encode(array("Error code" => "1","Message" => "has not number param","Data"=>""));
		}
		
		if($this->hasNumberLessThanZero($listGuess)){
			return json_encode(array("Error code" => "2","Message" => "has number less than 0","Data" => ""));
		}
		
		if($this->hasDuplicate($listGuess)){
			return json_encode(array("Error code" => "3","Message" => "has duplicate number","Data" => ""));
		}
		
		foreach ($listGuess as $item){
			if($item > $price){
				array_push($listHigher, $item);
			}else{
				array_push($listLowerEqual, $item);
			}
		}
		
		
		if(count($listLowerEqual) > 0){
			$listLowerEqual = $this->sortDESC($listLowerEqual);
		}
		
		if(count($listHigher) > 0){
			$listHigher = $this->sortASC($listHigher);
		}
		$listResult = $listLowerEqual;
		foreach ($listHigher as $item){
			array_push($listResult, $item);
		}
		return json_encode(array("Error code" => "0","Message" => "success","Data" => $listResult));
    }
    
    private function sortASC($list){
    	for($i=0; $i<count($list)-1; $i++){
    		for($j=$i+1; $j<count($list); $j++){
    			if($list[$i] > $list[$j]){
    				$temp = $list[$i];
    				$list[$i] = $list[$j];
    				$list[$j] = $temp;
    			}
    		}
    	}
    	return $list;
    }
    
    private function sortDESC($list){
    	for($i=0; $i<count($list)-1; $i++){
    		for($j=$i+1; $j<count($list); $j++){
    			if($list[$i] <  $list[$j]){
    				$temp = $list[$i];
    				$list[$i] = $list[$j];
    				$list[$j] = $temp;
    			}
    		}
    	}
    	return $list;
    }
    
    
    //Check an array has duplicate number
    private function hasDuplicate($listGuess){
    	for($i=0; $i<count($listGuess) - 1; $i++){
    		for($j=$i+1; $j<count($listGuess); $j++){
    			if($listGuess[$i] == $listGuess[$j]){
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
    
    private function hasNotNumberParam($price, $listGuess){
    	if(!is_numeric($price)){
    		return true;
    	}
    	foreach ($listGuess as $item){
    		if(!is_numeric($item)){
    			return true;
    		}
    	}
    	return false;
    }
    
    private function hasNotIntergerParam($array){
    	foreach ($array as $item){
    		if(!is_int($item)){
    			return true;
    		}
    	}
    	return false;
    }
    
    private function hasNumberLessThanZero($listGuess){
    	foreach ($listGuess as $item){
    		if($item < 0){
    			return true;
    		}
    	}
    	return false;
    }
    
    private function hasNotIntegerParam2DArray($array){
    	for($i=0;$i<count($array[0]); $i++){
    		if($this->hasNotIntergerParam($array[$i])){
    			return true;
    		}
    	}
    	return false;
    }
    
    private function hasDuplicateRow($array){
    	for($i=0; $i<count($array[0]); $i++){
    		if($this->hasDuplicate($array[$i])){
    			return true;
    		}
    	}
    	return false;
    } 
    
    private function hasDuplicateColumn($array){
    	for($i=0; $i<count($array[0]); $i++){
    		for($j=0; $j<count($array[0]) -1; $j++){
    			for($g=$j+1; $g<count($array[0]); $g++){
    				if($array[$i][$j] == $array[$i][$g]){
    					return true;
    				}
    			}
    		}
    	}
    	return false;
    }
    
    private function hasRowNot1To9($array){
    	for($i=0; $i<count($array[0]); $i++){
    		for($j=0; $j<count($array[0]); $j++){
    			if($array[$i][$j] < 1 || $array[$i][$j] > 9){
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
    private function hasColNot1To9($array){
    	for($i=0; $i<count($array[0]); $i++){ //col
    		for($j=0; $j<count($array[0]); $j++){ //row
    			if($array[$j][$i] < 1 || $array[$j][$i] > 9){
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
    private function getArray3x3($array, $fromRow, $fromCol){
    	$result = array();
    	for($i=$fromRow; $i<$fromRow+3; $i++){
    		for($j=$fromCol; $j<$fromCol+3; $j++){
    			array_push($result, $array[$i][$j]);
    		}
    	}
    	return $result;
    }
    
    public function doCheckSudoku9x9($array){
    	//TODO
    	//Check input
    	if($this->hasNotIntegerParam2DArray($array)){
    		return "has param not integer";
    	}
    	
    	//Check row
    		//Check duplicate
    		
		if($this->hasDuplicateRow($array)){
			return "has duplicate in row";
		}
		
		if($this->hasRowNot1To9($array)){
			return "has row not 1-9";
		}
		
    	
    	//Check column
    		//Check conflict
		if($this->hasDuplicateColumn($array)){
			return "has duplicate in column";
		}
		
		
		if($this->hasColNot1To9($array)){
			return "has column not 1-9";
		}
		
		$listChildArray = array();
		
		for($i=0; $i<count($array[0])-1; $i = $i+3){
			for($j=0; $j<count($array[0])-1; $j = $j+3){
				array_push($listChildArray, $this->getArray3x3($array, $i, $j));
			}
		}
		
		
		foreach ($listChildArray as $child){
			
			if($this->hasDuplicate($child)){
				return "array 3x3 has duplicate number";
			}
			
			foreach ($child as $item){
				if($item < 1 || $item > 9){
					return "array 3x3 not 1-9";
				}
			}
		}
		
		return "is sudoku 9x9";
    }
    
    
}